<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelStatistics extends JModel{
	
	function getParams(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('params');
		$query->from('#__extensions');
		$query->where("element='com_surveys'");
		$db->setQuery($query);		
		$db->query();
		$result_string = $db->loadAssocList();
		$result = json_decode($result_string["0"]["params"]);				
		return $result;
	}
	
	function getAllResponses($survey_id){
		$r_id = JRequest::getVar("r_id", "");
		$and = "";
		if($r_id != ""){
			$and = " and id=".$r_id;
		}
		$db =& JFactory::getDBO();
		$sql = "select count(*) from #__ijoomla_surveys_session where completed=1 and last_page_id=0 and published=1 and survey_id=".$survey_id.$and;
		$db->setQuery($sql);
		$db->query();
		$result = $db->loadResult();
		return $result;
	}
	
	function getAllQuestions($survey_id){
		$data = new JDate();
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('*');
		$query->from('#__ijoomla_surveys_questions');
		$query->where("survey_id=".$survey_id." and published=1 and published_up <= '".$data."' and ((published_down >= '".$data."') or published_down='0000-00-00 00:00:00')");
		$query->order("ordering asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();
		return $result;
	}
	
	function getStatistics(){
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEssay.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultiplePerRow.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOneAnswer.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOneAnswerMenu.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultipleAnswer.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOnePerRow.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEndedOneLine.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEndedMoreLines.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionConstant.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionDateTime.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionDateTime.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultiplePerRowMenus.php");
		require_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."questions.php");
		
		$params = $this->getParams();				
		
		$class_object = new Question();
		$survey_id = JRequest::getVar("s_id", "0");		
		$all_responses_count = $this->getAllResponses($survey_id);		
		$all_questions = $this->getAllQuestions($survey_id);
		$i = 1;
		$result = "";
		if(isset($all_questions) && is_array($all_questions) && count($all_questions)>0){					
			foreach($all_questions as $key=>$value){				
				$class = "Question".$class_object->getClass($value["question_type"]);
				$object = new $class();				
				$result .= $object->editResult($value["id"], $survey_id, $params, $i, $value["title"], $all_responses_count);
				$result .= "<br /><br />";
				$i++;
			}
		}
		return $result;
	}
	
	function editdetails(){
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."view_details.php");
		$class = new ViewDetails();
		$return = $class->edit();
		echo $return;
	}
}

?>
